package com.tools.ad.adscene;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import com.hm.adsbuisiness.bean.banner.AMNRawBannerAd;
import com.hm.adsbuisiness.bean.banner.AMRawBannerAd;
import com.hm.adsbuisiness.bean.banner.RawBannerAd;
import com.tools.ad.ADConstant;
import com.tools.ad.AdType;
import com.tools.ad.EntranceType;
import com.tools.ad.R;
import com.tools.ad.adscene.inner.CommonInterstitialActivityScene;

/**
 * desc: 模拟 插屏广告（将banner广告以插屏的形式展示）
 * date: 2018/11/12
 * author: ancun
 */
public class InterstitialOfBannerActivity extends InterstitialActivity {

    public static void showInterstitialAd(Context context, String entranceType, int placeId) {
        Intent intent = new Intent(context, InterstitialOfBannerActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(ENTRANCE_TYPE, entranceType);
        intent.putExtra(PLACE_ID, placeId);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ad_activity_banner_to_interstitial);

        initHomeKeyReceiver();
        initAdView();
    }

    private void initAdView() {
        Intent intent = getIntent();
        if (intent != null) {
            String mEntranceTypeStr = intent.getStringExtra(ENTRANCE_TYPE);
            mPlaceId = intent.getIntExtra(PLACE_ID, ADConstant.INVALID_ID);

            if (mPlaceId == ADConstant.INVALID_ID) {
                close();
                return;
            }

            mEntranceType = EntranceType.enumOf(mEntranceTypeStr);
            final RawBannerAd mActiveAd = (RawBannerAd) ((AdStrategyScene) mEntranceType.getAdScene()).getActiveAd();

            if (mActiveAd == null) {
                close();
                return;
            }

            View btn = findViewById(R.id.ad_banner_close_btn);
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mActiveAd instanceof AMRawBannerAd) {
                        ((CommonInterstitialActivityScene) mEntranceType.getAdScene()).onAdClose(AdType.AM_BANNER);
                    } else if (mActiveAd instanceof AMNRawBannerAd) {
                        ((CommonInterstitialActivityScene) mEntranceType.getAdScene()).onAdClose(AdType.AM_NEW_BANNER);
                    }

                    close();
                }
            });

            final View root = findViewById(R.id.root);
            root.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
                    root.getViewTreeObserver().removeOnPreDrawListener(this);

                    if (mActiveAd instanceof AMRawBannerAd) {
                        ((CommonInterstitialActivityScene) mEntranceType.getAdScene()).onADShow(AdType.AM_BANNER);
                    } else if (mActiveAd instanceof AMNRawBannerAd) {
                        ((CommonInterstitialActivityScene) mEntranceType.getAdScene()).onADShow(AdType.AM_NEW_BANNER);
                    }

                    return false;
                }
            });

            try {
                ViewGroup adContainer = findViewById(R.id.ad_banner_container);
                adContainer.removeAllViews();
                adContainer.addView(mActiveAd.getBannerView());
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    @Override
    public void close() {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
